/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    Room end,deadlyTrp;
    /**
     * Create all the rooms and link their exits together.
     */
    
    private void createRooms()
    {
        Room outside, theater, pub, lab, office, heaven, hell, stadium, gymnasium, teachingBuilding, lavatory, wall;
      
        // create the rooms
        outside = new Room("来到大学的正门外",new Thing("可乐",10));
        theater = new Room("来到演讲剧场");
        pub = new Room("来到校园酒吧,因为还没开始营业，顾客寥寥无几",new Thing("吧台上的红酒",-30));
        lab = new Room("来到计算机实验室遇到梅老师",new Thing("apple",20));
        office = new Room("来到计算管理办公室");
        heaven = new Room("误入隐藏出口1，看看周围有什么宝贝！",new Thing("伸腿瞪眼丸",100));
        hell = new Room("误入隐藏出口2",new Thing("马桶神的礼物",-50));
        stadium = new Room("来到体育场",new Thing("面包",20));
        gymnasium = new Room("来到体育馆",new Thing("遗失的水壶",-20));
        teachingBuilding = new Room("来到教学楼",new Thing("月永动",20));
        lavatory = new Room("来到厕所",new Thing("外卖",20));
        wall = new Room("来到学校附近的围墙");
        end = stadium;
        deadlyTrp = wall;
        // initialise room exits
        
        
        //outside.setExits(null, theater, lab, pub, heaven, hell);
        outside.setExits("east",theater);
        outside.setExits("south",lab);
        outside.setExits("west",pub);
        //theater.setExits(null, null, null, outside, null, null);
        theater.setExits("west",outside);
        theater.setExits("south",office);
        //pub.setExits(null, outside, null, null, null, null);
        pub.setExits("east",outside);
        pub.setExits("south",stadium);
        //lab.setExits(outside, office, null, null, null, null);
        lab.setExits("north",outside);
        lab.setExits("east",office);
        lab.setExits("south",teachingBuilding);
        lab.setExits("west",stadium);
        //office.setExits(null, null, null, lab, null, null);
        office.setExits("west",lab);
        office.setExits("north",theater);
        //heaven.setExits(null, null, null, null, null, outside);
        heaven.setExits("down",lavatory);
        //hell.setExits(null, null, null, null, outside, null);
        hell.setExits("up",lavatory);
        stadium.setExits("north",pub);
        stadium.setExits("east",lab);
        //预留位置设置出口
        gymnasium.setExits("east",teachingBuilding);
        gymnasium.setExits("north",stadium);
        teachingBuilding.setExits("north",lab);
        teachingBuilding.setExits("west",gymnasium);
        teachingBuilding.setExits("east",lavatory);
        teachingBuilding.setExits("south",wall);
        wall.setExits("north",teachingBuilding);
        lavatory.setExits("north",office);
        lavatory.setExits("west",teachingBuilding);
        lavatory.setExits("up",heaven);
        lavatory.setExits("down",hell);
        currentRoom = outside;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();/*从命令提示符里读取一个命令*/
            finished = processCommand(command);/*进入processCommand*/
        }
        System.out.println("感谢你玩这个游戏，再见！");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到“要迟到啦！”");
        System.out.println("“要迟到啦！”是一款无聊的冒险游戏.");
        System.out.println("如果你需要帮助，请输入“help”.");
        System.out.println();
        System.out.println("你在 " + currentRoom.getDescription());
        currentRoom.printExits();
    }
    
    /*public void printExists(){
        System.out.print("Exits: ");
        if(currentRoom.northExit != null) {
            System.out.print("north ");
        }
        if(currentRoom.eastExit != null) {
            System.out.print("east ");
        }
        if(currentRoom.southExit != null) {
            System.out.print("south ");
        }
        if(currentRoom.westExit != null) {
            System.out.print("west ");
        }
         if(currentRoom.upExit != null) {
            System.out.print("up ");
        }
         if(currentRoom.downExit != null) {
            System.out.print("down ");
        }
        System.out.println();
    }*/

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("我不理解你的意思。");
            return false;
        }

        Word commandWord = command.getCommandWord();
        switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case PICK:
                pick();
                break;
            case EAT:
                eat(command);
                break;
            case LOOK:
                look();
                break;
            case CHECK:
                check();
                break;
        }
        //if (commandWord.equals("help")) {
        //    printHelp();
        //}
        //else if (commandWord.equals("go")) {
        //    goRoom(command);
        //}
        //else if (commandWord.equals("quit")) {
        //    wantToQuit = quit(command);
        //}
        //else if (commandWord.equals("pick")) {
        //    pick();
        //}
        //else if (commandWord.equals("eat")) {
        //    eat(command);
        //}
        //else if (commandWord.equals("look")) {
        //    look();
        //}
        //else if (commandWord.equals("check")) {
        //    check();
        //}

        return wantToQuit;
    }
    
    private void eat(Command command){/*捡起来才能吃*/
        //Thing thing = player.bag.get(thingName);/*c从背包里找这个物品*/
        //if(thing != command.getSecondWord()){
        //     System.out.println("背包里没有这个东西。");
        //}
        
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("吃什么?");
            return;
        }
        String food = command.getSecondWord();
        player.eat(food);
        
    }
    
     private void look(){
        Thing thing = currentRoom.getThing();/*获得当前房间的物品*/
        if(thing != null){
            System.out.println("附近有 ： " + thing.getName() + " .");
        }else{
            System.out.println("附近没有东西 ！");
        }
    }
    
     private void check(){
        player.check();
     }
    
    private void pick(){
        Thing thing = currentRoom.getThing();/*获得当前房间的物品*/
        if(thing != null){
            player.pick(thing);
            currentRoom.removeThing();
            System.out.println("玩家捡起了： " + thing.getName() + " .");
        }else{
            System.out.println("附近没有东西可以被捡起.");
        }
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("离体育课课考勤还有五分钟");
        System.out.println("此时你在大学门口");
        System.out.println("请及时赶往体育场报道");
        System.out.println("小心被巡逻的保安抓到");
        System.out.println();
        System.out.println("你的命令是:");
        System.out.println("   "+ CommandWords.allCommandWords());
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪儿?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goRoom(direction);
        
        if (nextRoom == null) {
            System.out.println("这里没有出口!");
        }
        else {
            currentRoom = nextRoom;
            gameOver();
            dead();
            player.step();/*走一步减掉10体力*/
            System.out.println("你 " + currentRoom.getDescription());
            System.out.println("体力值消耗：-10 ." );
            player.showStrength();
            currentRoom.printExits();
        }
        
    }
    private void gameOver(){
        if(currentRoom == end){
            System.out.println("来到运动场，及时赶上了比赛！");
            System.out.println("恭喜通关！");
            System.exit(1);
        }
    }
    private void dead(){
        if(currentRoom == deadlyTrp){
            System.out.println("被巡逻的保安误以为逃课抓住");
            System.out.println("游戏失败！");
            System.exit(1);
        }
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("取消什么?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}
